<?php
/**
*
* @package MoltX QuickChat Installer
* @author $Author: moltendorf $
* @version $Id: install_molten_chat.php 184 2009-09-27 23:00:06Z moltendorf $
* @latest $URL: https://molten-chat.googlecode.com/svn/trunk/source/install_molten_chat.php $
* @copyright (c) 2005-2009 Matthew Oltendorf
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('mods/info_acp_molten');

if (!file_exists($phpbb_root_path . 'umil/umil.' . $phpEx))
{
	trigger_error('Please download the latest UMIL (Unified MOD Install Library) from: <a href="http://www.phpbb.com/mods/umil/">phpBB.com/mods/umil</a>', E_USER_ERROR);
}

// We only allow a founder to install this MOD
if ($user->data['user_type'] != USER_FOUNDER)
{
	if ($user->data['user_id'] == ANONYMOUS)
	{
		login_box('', 'LOGIN');
	}

	trigger_error('NOT_AUTHORISED');
}

if (!class_exists('umil'))
{
	include($phpbb_root_path . 'umil/umil.' . $phpEx);
}

$umil = new umil(true);

$mod = array(
	'name'		=> 'MoltX QuickChat 2.1 for phpBB 3',
	'version'	=> '2.1.0-a8.1',
	'config'	=> 'molten_chat_version',
	'enable'	=> 'molten_chat_enable',
);

// For future use.
$install = false;
$upgrade = false;

if (confirm_box(true))
{
	// Install the base 2.1.0-a6 version
	if (!$umil->config_exists($mod['config']))
	{
		// We are installing.
		$install = true;

		// We must handle the version number ourselves.
		$umil->config_add($mod['config'], $mod['version']);

		$umil->permission_add(array(
			array('a_molten_x_settings', 1),
			array('a_molten_chat_settings', 1),
			array('m_molten_chat_moderate_chat', 1),
			array('u_molten_chat_user_view', 1),
			array('u_molten_chat_user_send', 1),
			array('u_molten_chat_user_view_moderated', 1),
			array('u_molten_chat_user_send_moderated', 1),
		));

		$umil->permission_set(array(
			array('ROLE_ADMIN_FULL', 'a_molten_chat_settings', 'role'),
			array('ROLE_USER_FULL', 'u_molten_chat_user_view', 'role'),
			array('ROLE_USER_STANDARD', 'u_molten_chat_user_view', 'role'),
			array('ROLE_USER_NOPM', 'u_molten_chat_user_view', 'role'),
			array('ROLE_USER_NOAVATAR', 'u_molten_chat_user_view', 'role'),
			array('ADMINISTRATORS', 'u_molten_chat_user_view', 'group'),
			array('GLOBAL_MODERATORS', 'u_molten_chat_user_view', 'group'),
			array('GUESTS', 'u_molten_chat_user_view', 'group'),
			array('REGISTERED', 'u_molten_chat_user_view', 'group'),
			array('ROLE_USER_FULL', 'u_molten_chat_user_send', 'role'),
			array('ROLE_USER_STANDARD', 'u_molten_chat_user_send', 'role'),
			array('ROLE_USER_NOPM', 'u_molten_chat_user_send', 'role'),
			array('ROLE_USER_NOAVATAR', 'u_molten_chat_user_send', 'role'),
			array('ADMINISTRATORS', 'u_molten_chat_user_send', 'group'),
			array('GLOBAL_MODERATORS', 'u_molten_chat_user_send', 'group'),
			array('REGISTERED', 'u_molten_chat_user_send', 'group')
		));

		// Set installed to true.
		$umil->config_add('molten_chat_installed', '1', '0');

		$umil->config_add('molten_chat_cache_type', '0', '0');
		$umil->config_add('molten_chat_storage_type', '0', '0');
		$umil->config_add('molten_chat_enable', '0', '0');
		$umil->config_add('molten_chat_messages_reply', '1', '0');
		$umil->config_add('molten_chat_messages_topic', '1', '0');
		$umil->config_add('molten_chat_cache_initial_retrieve', '100', '0');
		$umil->config_add('molten_chat_cache_message_length', '512', '0');
		$umil->config_add('molten_chat_cache_level_messages', '50', '0');
		$umil->config_add('molten_chat_cache_level_count', '5', '0');
		$umil->config_add('molten_chat_cache_level_age', '5', '0');
		$umil->config_add('molten_chat_cache_update', '30', '0');
		$umil->config_add('molten_chat_cache_connections', '1', '0');

		$umil->module_add(array(
			// Add "MoltX" tab.
			array('acp', 0, 'MOLTEN_X'),

			// Add "QuickChat" category to "MoltX" tab.
			array('acp', 'MOLTEN_X', 'MOLTEN_CHAT'),

			// Add "Update" module to "QuickChat" category.
			array('acp', 'MOLTEN_CHAT',
				array(
					'module_basename'	=> 'molten_chat',
					'module_langname'	=> 'MOLTEN_CHAT_UPDATE',
					'module_mode'		=> 'update',
					'module_auth'		=> 'acl_a_molten_chat_settings'
				)
			),

			// Add "General Settings" module to "QuickChat" category.
			array('acp', 'MOLTEN_CHAT',
				array(
					'module_basename'	=> 'molten_chat',
					'module_langname'	=> 'MOLTEN_CHAT_SETTINGS_GENERAL',
					'module_mode'		=> 'general',
					'module_auth'		=> 'acl_a_molten_chat_settings'
				)
			),

			// Add "Cache Settings" module to "QuickChat" category.
			array('acp', 'MOLTEN_CHAT',
				array(
					'module_basename'	=> 'molten_chat',
					'module_langname'	=> 'MOLTEN_CHAT_SETTINGS_CACHE',
					'module_mode'		=> 'cache',
					'module_auth'		=> 'acl_a_molten_chat_settings'
				)
			),

			// Add "Debug" module to "QuickChat" category.
			array('acp', 'MOLTEN_CHAT',
				array(
					'module_basename'	=> 'molten_chat',
					'module_langname'	=> 'MOLTEN_CHAT_DEBUG',
					'module_mode'		=> 'debug',
					'module_auth'		=> 'acl_a_molten_chat_settings'
				)
			)
		));
	}
	// Upgrade.
	elseif (version_compare($config['molten_chat_version'], $mod['version'], '<'))
	{
		// We are upgrading.
		$upgrade = true;

		// Versions in descending order. We avoid using break here because all steps after a true equation must be performed anyway.
		switch(true)
		{
			case version_compare($config['molten_chat_version'], '2.1.0-a4', '<'):
				// This was missing in the previous version.
				$umil->config_add('molten_chat_cache_type', '0', '0');
				$umil->config_add('molten_chat_enable', '0', '0');

				// Add configuration for new post and topic additional messages.
				$umil->config_add('molten_chat_messages_reply', '1', '0');
				$umil->config_add('molten_chat_messages_topic', '1', '0');

			case version_compare($config['molten_chat_version'], '2.1.0-a7', '<'):
				$umil->config_add('molten_chat_cache_initial_retrieve', '100', '0');
				$umil->config_add('molten_chat_cache_message_length', '512', '0');
				$umil->config_add('molten_chat_cache_level_messages', '50', '0');
				$umil->config_add('molten_chat_cache_level_count', '5', '0');
				$umil->config_add('molten_chat_cache_level_age', '5', '0');
				$umil->config_add('molten_chat_cache_update', '30', '0');
				$umil->config_add('molten_chat_cache_connections', '1', '0');

			case version_compare($config['molten_chat_version'], '2.1.0-a8', '<'):
				$umil->permission_set(array(
					array('ROLE_ADMIN_FULL', 'a_molten_chat_settings', 'role'),
					array('ROLE_USER_FULL', 'u_molten_chat_user_view', 'role'),
					array('ROLE_USER_STANDARD', 'u_molten_chat_user_view', 'role'),
					array('ROLE_USER_NOPM', 'u_molten_chat_user_view', 'role'),
					array('ROLE_USER_NOAVATAR', 'u_molten_chat_user_view', 'role'),
					array('ADMINISTRATORS', 'u_molten_chat_user_view', 'group'),
					array('GLOBAL_MODERATORS', 'u_molten_chat_user_view', 'group'),
					array('GUESTS', 'u_molten_chat_user_view', 'group'),
					array('REGISTERED', 'u_molten_chat_user_view', 'group'),
					array('GLOBAL_MODERATORS', 'u_molten_chat_user_send', 'group')
				));

				$umil->config_add('molten_chat_storage_type', '0', '0');

				// The magic append function, adds the cache settings to the proper places.
				if ($umil->module_exists('acp', false, 'MOLTEN_CHAT_SETTINGS'))
				{
					$sql = 'SELECT parent_id AS module_id, module_id AS remove_id FROM ' . MODULES_TABLE . '
						WHERE module_class = \'acp\'
						AND module_langname = \'MOLTEN_CHAT_SETTINGS\'';

					// Remove this module from existing installations.
					$remove = true;
				}
				elseif ($umil->module_exists('acp', false, 'MOLTEN_CHAT'))
				{
					$sql = 'SELECT module_id FROM ' . MODULES_TABLE . '
						WHERE module_class = \'acp\'
						AND module_langname = \'MOLTEN_CHAT\'';
				}
				elseif ($umil->module_exists('acp', false, 'MOLTEN_X'))
				{
					$sql = 'SELECT module_id FROM ' . MODULES_TABLE . '
						WHERE module_class = \'acp\'
						AND module_langname = \'MOLTEN_X\'';
					$result = $db->sql_query($sql);
					$row = $db->sql_fetchrow($result);
					while ($row)
					{
						// Add "QuickChat" category.
						$umil->module_add('acp', (int) $row['module_id'], 'MOLTEN_CHAT');

						$row = $db->sql_fetchrow($result);
					}
					$db->sql_freeresult($result);
					unset ($result, $row);

					$sql = 'SELECT module_id FROM ' . MODULES_TABLE . '
						WHERE module_class = \'acp\'
						AND module_langname = \'MOLTEN_CHAT\'';
				}

				if (!empty($sql))
				{
					$parent = null;

					$add = array(
						// Add "Update" module to "QuickChat" category.
						array('acp', &$parent,
							array(
								'module_basename'	=> 'molten_chat',
								'module_langname'	=> 'MOLTEN_CHAT_UPDATE',
								'module_mode'		=> 'update',
								'module_auth'		=> 'acl_a_molten_chat_settings'
							)
						),

						// Add "General Settings" module to "QuickChat" category.
						array('acp', &$parent,
							array(
								'module_basename'	=> 'molten_chat',
								'module_langname'	=> 'MOLTEN_CHAT_SETTINGS_GENERAL',
								'module_mode'		=> 'general',
								'module_auth'		=> 'acl_a_molten_chat_settings'
							)
						),

						// Add "Cache Settings" module to "QuickChat" category.
						array('acp', &$parent,
							array(
								'module_basename'	=> 'molten_chat',
								'module_langname'	=> 'MOLTEN_CHAT_SETTINGS_CACHE',
								'module_mode'		=> 'cache',
								'module_auth'		=> 'acl_a_molten_chat_settings'
							)
						),

						// Add "Debug" module to "QuickChat" category.
						array('acp', &$parent,
							array(
								'module_basename'	=> 'molten_chat',
								'module_langname'	=> 'MOLTEN_CHAT_DEBUG',
								'module_mode'		=> 'debug',
								'module_auth'		=> 'acl_a_molten_chat_settings'
							)
						)
					);

					$result = $db->sql_query($sql);
					$row = $db->sql_fetchrow($result);
					if ($row)
					{
						// Add modules.
						do
						{
							if (!empty($remove))
							{
								// Remove "QuickChat Settings" so we can replace it with "General Settings."
								$umil->module_remove('acp', false, (int) $row['remove_id']);
							}

							// This will allow us to use $add without changing it directly.
							$parent = (int) $row['module_id'];

							$umil->module_add($add);

							$row = $db->sql_fetchrow($result);
						}
						while ($row);

						$success = true;
					}
				}

				if (empty($success))
				{
					// Re-add it all.
					$umil->module_add(array(
						// Add "MoltX" tab.
						array('acp', 0, 'MOLTEN_X'),

						// Add "QuickChat" category to "MoltX" tab.
						array('acp', 'MOLTEN_X', 'MOLTEN_CHAT'),

						// Add "Update" module to "QuickChat" category.
						array('acp', 'MOLTEN_CHAT',
							array(
								'module_basename'	=> 'molten_chat',
								'module_langname'	=> 'MOLTEN_CHAT_UPDATE',
								'module_mode'		=> 'update',
								'module_auth'		=> 'acl_a_molten_chat_settings'
							)
						),

						// Add "General Settings" module to "QuickChat" category.
						array('acp', 'MOLTEN_CHAT',
							array(
								'module_basename'	=> 'molten_chat',
								'module_langname'	=> 'MOLTEN_CHAT_SETTINGS_GENERAL',
								'module_mode'		=> 'general',
								'module_auth'		=> 'acl_a_molten_chat_settings'
							)
						),

						// Add "Cache Settings" module to "QuickChat" category.
						array('acp', 'MOLTEN_CHAT',
							array(
								'module_basename'	=> 'molten_chat',
								'module_langname'	=> 'MOLTEN_CHAT_SETTINGS_CACHE',
								'module_mode'		=> 'cache',
								'module_auth'		=> 'acl_a_molten_chat_settings'
							)
						),

						// Add "Debug" module to "QuickChat" category.
						array('acp', 'MOLTEN_CHAT',
							array(
								'module_basename'	=> 'molten_chat',
								'module_langname'	=> 'MOLTEN_CHAT_DEBUG',
								'module_mode'		=> 'debug',
								'module_auth'		=> 'acl_a_molten_chat_settings'
							)
						)
					));
				}
		}


		// Handle all upgrades from previous version to this version here.

		// We must handle the version number ourselves. Forced.
		set_config($mod['config'], $mod['version']);
	}

	if ($install or $upgrade)
	{
		// Our final action, we purge the board cache.
		$umil->cache_purge('template');
		$umil->cache_purge('theme');
		$umil->cache_purge();
	}

	// We are done.
	trigger_error('MOLTEN_CHAT_' . (($install and !$upgrade) ? 'INSTALLED' : ($upgrade ? 'UPGRADED' : 'LATEST')));
}
else
{
	confirm_box(false, 'INSTALL_MOLTEN_CHAT');
}

// Shouldn't get here.
redirect($phpbb_root_path . $user->page['page_name']);